package com.hhp.cloud.shop.admin.controller;

import java.util.Arrays;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.admin.pojo.entity.OauthClientEntity;
import com.hhp.cloud.shop.admin.service.OauthClientService;

import com.hhp.cloud.shop.common.core.result.R;



/**
 * 
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-23 10:25:33
 */
@RestController
@RequestMapping("/api/v1/admin/oauthClient")
public class OauthClientController {
    @Autowired
    private OauthClientService oauthClientService;

    @ApiOperation(value = "客户端列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "query", dataType = "String")
    })
    @GetMapping
    public R list(Integer page, Integer limit, String clientId){
        return R.ok().put("data", oauthClientService.pageMaps(
                new Page<>(page, limit),
                new LambdaQueryWrapper<OauthClientEntity>()
                        .eq(StrUtil.isNotBlank(clientId), OauthClientEntity::getClientId, clientId)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "客户端详情")
    @ApiImplicitParam(name = "clientId", value = "客户端id", required = true, paramType = "path", dataType = "String")
    @GetMapping("/{clientId}")
    public R info(@PathVariable String clientId){
        return R.ok().setData(oauthClientService.getById(clientId));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增客户端")
    @ApiImplicitParam(name = "client", value = "实体JSON对象", required = true, paramType = "body", dataType = "OauthClientEntity")
    @PostMapping
    public R save(@RequestBody OauthClientEntity oauthClient){
		oauthClientService.save(oauthClient);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端id", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "client", value = "实体JSON对象", required = true, paramType = "body", dataType = "OauthClientDetails")
    })
    @PutMapping(value = "/{clientId}")
    public R update(@PathVariable String clientId, @RequestBody OauthClientEntity oauthClient){
		oauthClientService.updateById(oauthClient);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除客户端")
    @ApiImplicitParam(name = "clientIds", value = "clientId数组", required = true, paramType = "query", dataType = "String")
    @DeleteMapping
    public R delete(@RequestBody String[] clientIds){
		oauthClientService.removeByIds(Arrays.asList(clientIds));

        return R.ok();
    }

    @ApiOperation(hidden = true, value = "获取 OAuth2 客户端认证信息", notes = "Feign 调用")
    @GetMapping("/getOAuth2ClientById")
    public R getOAuth2ClientById(@RequestParam String clientId) {
        OauthClientEntity client = oauthClientService.getById(clientId);
        Assert.isTrue(client!=null, "OAuth2 客户端不存在");
        return R.ok().setData(client);
    }

}
